
import { achieveType, actionType, assetsType, attribute, carItem, companyType, diseaseType, eduType, figureType, gambleType, hospitalType, houseType, itemType, lineEventType, randomEventType, rankType, schoolType, skillType, suduType } from '../game/gameType';
import { ccs } from './services';
//负责读取游戏的配置数据
const fileArr = [
    'workType',
    "attribute",
    "assets",
    "action",
    "card",
    "questions",
    "edu",
    "lineEvent",
    "school",
    "figure",
    "relationAction",
    "company",
    "gamble",
    "disease",
    "hospital",
    "easy",
    "hard",
    "extreme",
    "medium",
    'randomEvent',
    "achieve",
    "rank", //排名
]

export class dataServices {

    gameJson = {
        'workType': [],
        "house": [],
        "attribute": [],
        "action": [],
        "card": [],
        "questions": [],
        "edu": [],
        "assets": [], 
        "lineEvent": [],
        "school": [],
        "figure": [],
        "relationAction": [],
        "company": [],
        "gamble": [],
        "disease": [],
        "hospital": [],
        "easy": [],
        "hard": [],
        "extreme": [],
        "medium": [],
        "randomEvent": [],
        "achieve": [],
        "rank": [],
    };
    
    //读取所有的文件
    async initJson() {
        fileArr.forEach(async val => {
            this.gameJson[val] = (await ccs.resMgr.loadRes<cc.JsonAsset>('gameData/'+val, cc.JsonAsset)).json;
            cc.log(val+'数据初始化完毕');
        })
    }
    
    getJsonData(dataName: string) {
        return JSON.parse(JSON.stringify(this.gameJson[dataName]));
    }
    getDataById(dataName: string, id: number | string) {
        return this.getJsonData(dataName).find(val => val.id == id);
    }
    getDataByAge(dataName: string, age: number | string) {
        return this.getJsonData(dataName).find(val => val.age <= age && val.maxAge >= age);
    }
    getDataByType(dataName: string, type: string | number) {
        return this.getJsonData(dataName).filter(val => val.type == type);
    }
    getItemsByType(type: string) {
        return this.getJsonData("items").filter(val => val.type == type);
    }
    
    getItemsById(id: number): itemType {
        return this.getDataById("items", id);
    }
    //获取属性名称
    getAttributeNameById(id: string | number): attribute{
        return this.getDataById("attribute", id);
    }
    //获取可做事情
    getEventList(id: number) {
        return this.gameJson.action.filter(val => val.location == id);
    }
    //获取证书
    getCardById(id: string | number) {
        return this.getDataById("card", id);
    }
    getCardByType() {
        return this.getJsonData('card').filter(val => val.type == 'card');
    }
    getAllCard() {
        return this.gameJson.card;
    }
    //获取考试题目
    getQuestionById(id: string) {
        return this.getJsonData("questions").filter(val => val.type == id);
    }
    //获取学历
    getEduById(id: number | string): eduType {
        return this.getDataById("edu", id);
    }
    //获取行动
    getAllAction(): actionType[] {
        return this.getJsonData('action');
    }
    //根据行动类型获取
    getActionByType(type: string): actionType {
        return this.getDataByType("action", type);
    }
    getAllAssets(): assetsType[] {
        return this.gameJson["assets"];
    }
    getAssetsByType(type: string): assetsType[] {
        return this.getDataByType("assets", type);
    }
    getAssetsById(id: number): assetsType {
        return this.getDataById("assets", id);
    }
   
    getLineEvent(age: number): lineEventType {
        return this.getDataByAge("lineEvent", age);
    }
    //根据年龄获取学习
    getEduByAge(age: number): eduType {
        let arr = this.getDataByAge("edu", age);
        if(arr) {
            return arr;
        }
        return this.getJsonData("edu")[0];
    }
    //获取可用学校
    getSchoolByEdu(edu: string): schoolType[] {
        return this.getJsonData('school').filter(val => val.type == edu);
    }
    
    //获取人物
    getFigure(startRange: number, endRange: number): figureType[] {
        return this.getJsonData('figure').slice(startRange, endRange - startRange);
    }
    //随机人物
    getRandomFigure(): figureType {
        let index = Math.trunc(Math.random() * 300);
        return this.gameJson['figure'].slice(index, index + 1)[0];
    }
    //获取亲密度行为
    getRelationActions() {
        return this.getJsonData("relationAction");
    }

    //获取公司的
    getCompanyList(): companyType[] {
        return this.getJsonData("company");
    }
    getCompanyById(id: string | number): companyType {
        return this.getDataById("company", id);
    }
    getCompanyIndex(id: number): number {
        return this.getJsonData('company').findIndex(val => val.id == id);
    }
    getWorkIndex(id: number): number {
        return this.getJsonData('workType').findIndex(val => val.id == id);
    }
    //获取赌博
    getGambleList(): gambleType[] {
        return this.getJsonData("gamble");
    }
    getGambleListById(id): gambleType {
        return this.getDataById("gamble", id);
    }
    //获取疾病
    getDisease(): diseaseType[] {
        return this.getJsonData('disease');
    }
    getDiseaseById(id): diseaseType {
        return this.getDataById("disease", id);
    }
    getDiseaseByLevel(level): diseaseType {
        let list = this.getDisease().filter(val => val.level === level);
        return list[Math.floor(Math.random() * list.length)];
    }
    //获取医院
    getHospital(): hospitalType[] {
        return this.getJsonData('hospital');
    }
    getHospitalById(id): hospitalType {
        return this.getDataById("hospital", id);
    }
    getHospitalByLevel(level): hospitalType {
        return this.getHospital().find(val => val.level == level);
    }
    //获取数独的数据
    getSuduData(diffcult: string): suduType {
        let list = this.getJsonData(diffcult).data;
        let idx = Math.floor(Math.random() * list.length);
        return list[idx];
    }
    //随机事件
    getRandomEvent(): randomEventType[] {
        return this.getJsonData("randomEvent");
    }
    getHappenRandomEvent(per): randomEventType[] {
        return this.getJsonData("randomEvent").filter(val => val.happen >= per);
    }
    //获取成就列表
    getAchieveList(): achieveType[] {
        return this.getJsonData("achieve");
    }
    getRankList(): rankType[] {
        return this.getJsonData('rank');
    }
    getRankById(id: number): rankType {
        return this.getJsonData('rank').find(val => val.id === id);
    }
}
